import java.util.ArrayList;
import java.util.Collections;




public class cTestPotAwarding {
	public static void main(String[] args) {
		
	
		class cRank implements Comparable<cRank>{
			public int playerIndex;
			public int handRank;
			public int chips;
			public cRank(int i, int r, int c){
				playerIndex = i;
				handRank = r;
				chips = c;
			}
			
			public int compareTo(cRank another) {
				
				if((this.handRank - another.handRank) == 0)
					return another.chips - this.chips;
				else
					return this.handRank - another.handRank;
			}
		}
		
		int currentPot = 1000;
		
		ArrayList<cRank> playerRanks = new ArrayList();
		playerRanks.add(new cRank(0,10,250));
		playerRanks.add(new cRank(1,5,250));
		playerRanks.add(new cRank(2,3,250));
		playerRanks.add(new cRank(3,10,250));
		Collections.sort(playerRanks);
		for(int i = 0; i < 4; i ++){
			System.out.println("Player " + (playerRanks.get(i).playerIndex+1) + " Rank " + playerRanks.get(i).handRank);
		}
		
		ArrayList<cRank> first = new ArrayList();
		ArrayList<cRank> second = new ArrayList();
		ArrayList<cRank> third = new ArrayList();
		ArrayList<cRank> fourth = new ArrayList();
		// Assumes sorted ranks
		for(int i = 3; i >= 0; i --){
			if(first.size() == 0 || (playerRanks.get(i).handRank == first.get(0).handRank) )
				first.add(playerRanks.get(i));
			else{
				if(second.size() == 0 || (playerRanks.get(i).handRank == second.get(0).handRank))
					second.add(playerRanks.get(i));
				else{
					if(third.size() == 0 || (playerRanks.get(i).handRank == third.get(0).handRank) ){
						third.add(playerRanks.get(i));
					}
					else{
						if(fourth.size() == 0 || (playerRanks.get(i).handRank == fourth.get(0).handRank)){
							fourth.add(playerRanks.get(i));
						}
					}
				}
			}
		}
	
		
		//* new awarding 
		int amountAwarded = 0;
		int playersAwarded = 0;
		ArrayList<cPlayer> playerList = new ArrayList();
		cPlayer p1 = new cPlayer(0);
		cPlayer p2 = new cPlayer(0);
		cPlayer p3 = new cPlayer(0);
		cPlayer p4 = new cPlayer(0);
		playerList.add(p1);
		playerList.add(p2);
		playerList.add(p3);
		playerList.add(p4);
		
		for(int i = 0; i < first.size(); i++){
			//award the first place people
			int chipsToAward = 0;
			int chipsToTake = first.get(i).chips;
			System.out.println("Doing player " + (first.get(i).playerIndex+1) + ": taking " + chipsToTake);
		//collect chips	
			for(int j = i; j < first.size(); j++){
				first.get(j).chips -= chipsToTake;
				chipsToAward+= chipsToTake;
			}
			for(int j = 0; j < second.size(); j++){
				second.get(j).chips -= chipsToTake;
				chipsToAward+= chipsToTake;
			}
			for(int j = 0; j < third.size(); j++){
				third.get(j).chips -= chipsToTake;
				chipsToAward+= chipsToTake;
			}
			for(int j = 0; j < fourth.size(); j++){
				fourth.get(j).chips -= chipsToTake;
				chipsToAward+= chipsToTake;
			}
	    //award chips
			int tmp = chipsToAward/(first.size()-i);
			for(int j = i; j < first.size(); j++){
				playerList.get(first.get(j).playerIndex).awardChips(tmp);
				amountAwarded += tmp;
			}
			playersAwarded++;
		}
		if(amountAwarded < currentPot){
			for(int i = 0; i < second.size(); i++){
				//award the first place people
				int chipsToAward = 0;
				int chipsToTake = second.get(i).chips;
				System.out.println("Doing player " + (second.get(i).playerIndex+1) + ": taking " + chipsToTake);
			//collect chips	
				for(int j = i; j < second.size(); j++){
					second.get(j).chips -= chipsToTake;
					chipsToAward+= chipsToTake;
				}
				for(int j = 0; j < third.size(); j++){
					third.get(j).chips -= chipsToTake;
					chipsToAward+= chipsToTake;
				}
				for(int j = 0; j < fourth.size(); j++){
					fourth.get(j).chips -= chipsToTake;
					chipsToAward+= chipsToTake;
				}
		    //award chips
				int tmp = chipsToAward/(second.size()-i);
				for(int j = i; j < second.size(); j++){
					playerList.get(second.get(j).playerIndex).awardChips(tmp);
					amountAwarded += tmp;
				}
				playersAwarded++;
			}
		}
		if(amountAwarded < currentPot){
			for(int i = 0; i < third.size(); i++){
				//award the third place people
				int chipsToAward = 0;
				int chipsToTake = third.get(i).chips;
				System.out.println("Doing player " + (third.get(i).playerIndex+1) + ": taking " + chipsToTake);
			//collect chips	
				for(int j = i; j < third.size(); j++){
					third.get(j).chips -= chipsToTake;
					chipsToAward+= chipsToTake;
				}
				for(int j = 0; j < fourth.size(); j++){
					fourth.get(j).chips -= chipsToTake;
					chipsToAward+= chipsToTake;
				}
		    //award chips
				int tmp = chipsToAward/(third.size()-i);
				for(int j = i; j < third.size(); j++){
					playerList.get(third.get(j).playerIndex).awardChips(tmp);
					amountAwarded += tmp;
				}
				playersAwarded++;
			}
		}
		if(amountAwarded < currentPot){
			for(int i = 0; i < fourth.size(); i++){
				//award the third place people
				int chipsToAward = 0;
				int chipsToTake = fourth.get(i).chips;
				System.out.println("Doing player " + (fourth.get(i).playerIndex+1) + ": taking " + chipsToTake);
			//collect chips	
				for(int j = i; j < fourth.size(); j++){
					fourth.get(j).chips -= chipsToTake;
					chipsToAward+= chipsToTake;
				}
		    //award chips
				int tmp = chipsToAward/(fourth.size()-i);
				for(int j = i; j < fourth.size(); j++){
					playerList.get(fourth.get(j).playerIndex).awardChips(tmp);
					amountAwarded += tmp;
				}
				playersAwarded++;
			}
		}
		
		ArrayList<cPlayer> playersList = new ArrayList();
		cPlayer pl1 = new cPlayer(100);
		p1.takeChips(50);
		
		cPlayer pl2 = new cPlayer(25);
		p2.takeChips(25);
		
		cPlayer pl3 = new cPlayer(0);
		p3.inHand = false;
		
		cPlayer pl4 = new cPlayer(0);
		p4.inHand = false;
		
		playersList.add(pl1);
		playersList.add(pl2);
		playersList.add(pl3);
		playersList.add(pl4);
		
		int maxBet = 0;
    	int secondMaxBet = 0;
    	for(int i = 0; i < 4; i++){
    		if(playersList.get(i).inHand && playersList.get(i).inGame){
    			int curPlayerChips = playersList.get(i).getChipsInRound();
    			if(curPlayerChips >= maxBet){
    				secondMaxBet = maxBet;
    				maxBet = curPlayerChips;
    			}
    			else if(curPlayerChips < maxBet && curPlayerChips > secondMaxBet){
    				secondMaxBet = curPlayerChips;
    			}
    		}
    	}
    	
    	System.out.println( "maxBet: " + maxBet);
    	System.out.println( "secondMaxBet: " + secondMaxBet);
    	
    	for(int i = 0; i < 4; i++){
    		if(playersList.get(i).inHand && playersList.get(i).inGame){
	    		if(playersList.get(i).getChipsInRound() > secondMaxBet && playersList.get(i).getChipsInRound() <= maxBet){
	    			int refund = playersList.get(i).getChipsInRound() - secondMaxBet;
	    			System.out.println( "Player " +(i+i) + " getting refund of " + refund);
	    			playersList.get(i).refundChips(refund);
	    		}
    		}
    	}
		
		for(int i = 0; i < playersList.size(); i ++){
			System.out.println("Player " + (i + 1) + " gets " + playerList.get(i).getChipAmount());
		}
		
	}
}
